<template>
  <!-- 充值审核 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          查询
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="add-box">
      <el-radio-group v-model="queryParams.status" size="mini" @input="checkedStatus">
        <el-radio-button :label="null">所有</el-radio-button>
        <el-radio-button :label="1">待审核</el-radio-button>
        <el-radio-button :label="2">审核通过</el-radio-button>
        <el-radio-button :label="3">审核拒绝</el-radio-button>
      </el-radio-group>
      <el-button type="primary" size="mini" @click="addRecharge">会员充值</el-button>
    </div>
    <!-- 表格 -->
    <el-table v-loading="loading" border :data="dataList">
      <el-table-column label="充值编号" align="center" prop="id" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="充值金额" align="center" prop="amount" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status | ctRechargeAudits }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="changeAuditsStatus(scope.row, 'success')"
            v-if="scope.row.status == 1">通过</el-button>
          <el-button type="text" size="mini" @click="changeAuditsStatus(scope.row, 'fail')"
            v-if="scope.row.status == 1">拒绝</el-button>
          <el-button type="text" size="mini" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页  -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize" @pagination="getList" />

    <!-- 拒绝 -->
    <el-dialog title="审核拒绝" :visible.sync="refuseShow" width="40%" :close-on-click-modal="false">
      <el-form ref="refuseForm" :model="refuseForm" label-width="100px" :rules="refuseRules">
        <el-form-item label="拒绝理由" prop="remark">
          <el-input v-model="refuseForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="refuseHandleSure">确 定</el-button>
        <el-button @click="refuseResetForm('refuseForm')">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog title="会员充值" :visible.sync="addShow" width="40%" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" label-width="100px" :rules="rules">
        <el-form-item label="用户昵称" prop="userId">
          <el-select v-model="addForm.userId" placeholder="请选择用户">
            <el-option v-for="item in userLists" :key="item.userId" :label="item.nickName" :value="item.userId" />
          </el-select>
        </el-form-item>
        <el-form-item label="充值金额" prop="amount">
          <el-input v-model="addForm.amount"></el-input>
        </el-form-item>
        <el-form-item label="付款凭证" prop="paymentVoucher">
          <Image-upload v-model="addForm.paymentVoucher" :limit="1" :isShowTip="false" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="addHandleSure">确 定</el-button>
        <el-button @click="resetForm('addForm')">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog title="详情" :visible.sync="detailShow" width="50%" :close-on-click-modal="false">
      <el-form :model="detailRow" label-width="100px">
        <div class="flex">
          <div>
            <el-form-item label="充值编号">{{ detailRow.id || "-" }}</el-form-item>
            <el-form-item label="充值金额"> {{ detailRow.amount || 0 }}元 </el-form-item>
            <el-form-item label="创建时间">{{ detailRow.createTime || "无" }}</el-form-item>
          </div>
          <div>
            <el-form-item label="用户名">{{ detailRow.nickName || "-" }}</el-form-item>
            <el-form-item label="审核状态">{{ detailRow.status | ctRechargeAudits }}</el-form-item>
          </div>
        </div>
        <el-form-item label="付款截图">
          <el-image v-for="item in detailRow.paymentVoucher" :key="item" :src="baseUrl + item"
            :preview-src-list="[baseUrl + item]" style="width: 100px; height: 70px" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="detailShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { accountLogList, accountLog, passOrRefuse } from "@/api/auditsManagement/rechargeAudits";
import { getMemberList } from "@/api/financialManagement/orderManagement";

export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false, // 遮罩层
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataList: [], // 充值管理表格数据
      // 查询参数
      queryParams: {
        status: null,
        params: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      detailRow: {},//详情内容
      detailShow: false,//详情弹窗开关
      addShow: false,//添加弹窗开关
      addForm: {},//添加的内容
      rules: {//添加表单验证
        userId: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        amount: [
          { required: true, message: '请输入充值金额', trigger: 'change' }
        ],
        paymentVoucher: [
          { required: true, message: '请上传付款凭证', trigger: 'change' }
        ],
      },
      userLists: [],//用户列表
      refuseShow: false,//拒绝弹窗
      refuseForm: {},//拒绝的表单
      refuseRules: {
        remark: [
          { required: true, message: '请输入拒绝理由', trigger: 'change' }
        ],
      },
    };
  },
  created() {
    //获取表格数据
    this.getList();
  },
  methods: {
    /**修改选中的状态 */
    checkedStatus(e) {
      this.queryParams.status = e;
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /**确认拒绝 */
    refuseHandleSure() {
      this.$refs['refuseForm'].validate((valid) => {
        if (valid) {
          passOrRefuse({ id: this.refuseForm.id, status: '3', remark: this.refuseForm.remark }).then(res => {
            if (200 == res.code) {
              this.getList();
              this.refuseShow = false;
              this.$message.success('审核拒绝！');
            }
          })
        }
      });
    },
    /**关闭拒绝对话框 */
    refuseResetForm(formName) {
      this.$refs[formName].resetFields();
      this.refuseShow = false;
    },
    /**获取用户列表 */
    getMemberLists() {
      getMemberList().then(res => {
        if (200 == res.code) {
          this.userLists = res.data;
        }
      })
    },
    /**确认添加 */
    addHandleSure() {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          accountLog(this.addForm).then(res => {
            if (200 == res.code) {
              this.getList();
              this.addShow = false;
              this.$message.success('会员充值成功！');
            }
          })
        }
      });
    },
    /**关闭添加对话框 */
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.addShow = false
    },
    /**打开添加弹窗 */
    addRecharge() {
      this.addShow = true;
      this.addForm = {};
      this.getMemberLists();
    },
    /**查看详情 */
    handleView(row) {
      this.detailRow = Object.assign({}, row);
      this.detailRow.paymentVoucher = this.detailRow.paymentVoucher.split(',');
      this.detailShow = true;
    },
    /**审核通过 & 打开拒绝弹窗 */
    changeAuditsStatus(row, type) {
      if ('success' == type) {
        this.$modal
          .confirm("是否确认审核通过?")
          .then(() => {
            passOrRefuse({ id: row.id, status: '2' }).then(res => {
              if (200 == res.code) {
                this.getList();
                this.$message.success('审核通过！');
              }
            })
          })
          .catch(() => { });
      } else {
        this.refuseForm = row;
        this.refuseShow = true;
      }

    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      accountLogList(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>

<style lang="scss" scoped>
.add-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.flex {
  display: flex;

  >div {
    flex: 1;
  }
}

.el-select {
  width: 100%;
}
</style>
